package com.study.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WordCount {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local[2]").setAppName("count")
    val sc = new SparkContext(conf)

    val sourceRDD: RDD[String] = sc.textFile("file:///C:\\Users\\86131\\Desktop\\workdocument\\faureDetect\\wordCount.txt",2)

    val wordAndOne = sourceRDD.map(word => (word.trim, 1))
//      wordAndOne.top()
//        .join()
//      .groupBy(_._1).mapValues( iter => iter.map( x => x._2).sum)
//      .groupByKey(2).mapValues(_.sum)
      .reduceByKey(_ + _)
        .distinct()
//        .cogroup(rdd2)
        .foldByKey(10)(_ + _)  // 会在每个map分区中使用初始值与 key 相加
//        .aggregateByKey(10)(_ + _, _ + _)
        .aggregateByKey(10)(Math.max, _ + _)
      .saveAsTextFile("")
  }

}
